﻿using Groep2TINGHService;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1.AdminView
{
    /// <summary>
    /// Interaction logic for Contracten.xaml
    /// </summary>
    public partial class Contracten : MetroWindow
    {
        public Contracten()
        {
            InitializeComponent();

            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Company company = new Company();
            List<Company> lijstCompanies = myClient.GetAllCompanies();
            lijstCompanies.Add(company);
            foreach (Company r in lijstCompanies)
            {
                lstCompany.Items.Add(r.Name);
            }
            Contract contract = new Contract();
            List<Contract> LijstCompaniesStop = myClient.GetAllContracts();
            LijstCompaniesStop.Add(contract);
            foreach (Contract r in LijstCompaniesStop)
            {
                lstBedrijvenStopzetten.Items.Add(r.Id);
            }

            ServiceReference1.Service1Client myClient2 = new ServiceReference1.Service1Client();
            ContractFormula formula = new ContractFormula();
            List<ContractFormula> lijstFormula = myClient2.GetAllContractFormula();
            lijstFormula.Add(formula);
            foreach (ContractFormula r in lijstFormula)
            {
                cmbFormula.Items.Add(r.Description);
            }
            txtEinddatum.IsEnabled = false;
        }

        private void lstCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

        private void btnBevestig_Click(object sender, RoutedEventArgs e)
        {
             //controle of datum juist is
            if (Convert.ToDateTime(dtpStartDate.SelectedDate) < DateTime.Today)
            {
                MessageBox.Show("Startdatum kan niet in het verleden zijn.");
            }
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();

            Contract contract = new Contract(); 
            ContractFormula formula = new ContractFormula();
            Company company = new Company();
            contract.StartDate = Convert.ToDateTime(dtpStartDate.SelectedDate);
            formula.Description = cmbFormula.SelectedItem.ToString();
            contract.EndDate = Convert.ToDateTime(txtEinddatum.Text);
            company.Name = lstCompany.SelectedItem.ToString();

            int x =myClient.InsertContract(contract,formula,company);
            if (x > 0)
            {
                MessageBox.Show("Er is een contract aangemaakt");
            }
            else
            {
                MessageBox.Show("Er is een fout opgelopen zorg dat alle velden goed ingevuld zijn");
            }
            
            
            

   
        }

        private void cmbFormula_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            ContractFormula formula = new ContractFormula();
            formula.Description = cmbFormula.SelectedItem.ToString();
            ContractFormula f2 = new ContractFormula();
            f2 = myClient.Get_ContractDurationByDescription(formula);
            DateTime StartDatum = Convert.ToDateTime(dtpStartDate.SelectedDate);
            txtEinddatum.Text = Convert.ToString(StartDatum.AddMonths(Convert.ToInt32(f2.PeriodInMonths.ToString())));
        }

        private void lstBedrijvenStopzetten_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            string contractID = lstBedrijvenStopzetten.SelectedItem.ToString();
            Company company = new Company();
            ContractFormula formula = new ContractFormula();
            Contract contract = new Contract();

            contract = myClient.GetContractByCompany(contractID);
            txtStartDatumStop.Text = contract.StartDate.ToString();
            txtEindDatumStop.Text = contract.EndDate.ToString();

            company = myClient.GetContractByCompanyWithName(contractID);
            txtBedrijfsnaam.Text = company.Name;

            formula = myClient.GetContractByCompanyWithDescription(contractID);
            txtFormulaStop.Text = formula.Description;
           
        }

        private void btnVerwijderen_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            string contractID = lstBedrijvenStopzetten.SelectedItem.ToString();
            Contract contract = new Contract();
            int x = myClient.DeleteContract(contractID);
            if (x > 0)
            {
                MessageBox.Show("Contract verwijdert!");
                lstBedrijvenStopzetten.Items.Clear();
                List<Contract> LijstCompaniesStop = myClient.GetAllContracts();
                LijstCompaniesStop.Add(contract);
                foreach (Contract r in LijstCompaniesStop)
                {
                    lstBedrijvenStopzetten.Items.Add(r.Id);
                }
            }

            
        }

        private void txtEinddatum_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}
